package org.mayanjun.code.dalint.mybatis;


import org.mayanjun.code.dalint.Sharding;
import org.mayanjun.code.dalint.ShardingEntityAccessor;
import org.mayanjun.code.dalint.ThreadLocalCallerClassAware;
import org.mayanjun.code.dalint.handler.EntityAccessHandler;
import org.mayanjun.code.dalint.mybatis.dao.BasicDAO;
import org.mayanjun.code.dalmeta.entity.PersistableEntity;
import org.mayanjun.code.dalmeta.query.Query;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * BasicEntityHandler
 *
 * @author mayanjun
 */
public abstract class BasicEntityAccessHandler extends ThreadLocalCallerClassAware
        implements EntityAccessHandler, ShardingEntityAccessor {

	@Autowired
	private BasicDAO dao;

	@Override
	public <T extends PersistableEntity> List<T> query(Query<T> query) {
		return dao.query(query);
	}

	@Override
	public <T extends PersistableEntity> T queryOne(Query<T> query) {
		return dao.queryOne(query);
	}

	@Override
	public int update(PersistableEntity bean) {
		return dao.update(bean);
	}

	@Override
	public int update(PersistableEntity bean, Query<? extends PersistableEntity> query) {
		return dao.update(bean, query);
	}

	@Override
	public long save(PersistableEntity bean) {
		return dao.save(bean);
	}

	@Override
	public int delete(PersistableEntity bean) {
		return dao.delete(bean);
	}

	@Override
	public int delete(Query<? extends PersistableEntity> query) {
		return dao.delete(query);
	}

	@Override
	public <T extends PersistableEntity> T getExclude(PersistableEntity bean, String... excludeFields) {
		return dao.getExclude(bean, excludeFields);
	}

	@Override
	public <T extends PersistableEntity> T getInclude(PersistableEntity bean, String... includeFields) {
		return dao.getInclude(bean, includeFields);
	}

	@Override
	public <T extends PersistableEntity> List<T> query(Query<T> query, Sharding sharding) {
		return dao.query(query, sharding);
	}

	@Override
	public <T extends PersistableEntity> T queryOne(Query<T> query, Sharding sharding) {
		return dao.queryOne(query, sharding);
	}

	@Override
	public int update(PersistableEntity bean, Sharding sharding) {
		return dao.update(bean, sharding);
	}

	@Override
	public int update(PersistableEntity bean, Sharding sharding, Query<? extends PersistableEntity> query) {
		return dao.update(bean, sharding, query);
	}

	@Override
	public long save(PersistableEntity bean, Sharding sharding) {
		return dao.save(bean, sharding);
	}

	@Override
	public int delete(PersistableEntity bean, Sharding sharding) {
		return dao.delete(bean, sharding);
	}

	@Override
	public int delete(Query<? extends PersistableEntity> query, Sharding sharding) {
		return dao.delete(query, sharding);
	}

	@Override
	public <T extends PersistableEntity> T getExclude(PersistableEntity bean, Sharding sharding, String... excludeFields) {
		return dao.getExclude(bean, sharding, excludeFields);
	}

	@Override
	public <T extends PersistableEntity> T getInclude(PersistableEntity bean, Sharding sharding,
													  String... includeFields) {
		return dao.getInclude(bean, sharding, includeFields);
	}

	@Override
	public int getOrder() {
		return Integer.MAX_VALUE;
	}
}